package com.deyu.base;

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.deyu.domain.Student;
import com.deyu.service.ActivityService;
import com.deyu.service.ActivityTypeService;
import com.deyu.service.StudentService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


@SuppressWarnings("serial")
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	// =============== ModelDriven的支持 ==================

	protected T model;

	public BaseAction() {
		try {
			// 通过反射获取model的真实类型
			ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
			Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
			// 通过反射创建model的实例
			model = clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public T getModel() {
		return model;
	}
	//获取传入的值
    public String request(String s){
    	return ServletActionContext.getRequest().getParameter(s);
    }
	// =============== Service实例的声明 ==================

	@Resource
	protected StudentService studentService;
	@Resource
	protected ActivityService activityService;
	@Resource
	protected ActivityTypeService activityTypeService;
	/**
	 * 获取当前登录的用户
	 * 
	 * @return
	 */
	protected Student getCurrentStudent() {
		return (Student) ActionContext.getContext().getSession().get("student");
	}

	// ============== 分页用的参数 =============

	protected int pageNum = 1; // 当前页
	protected int pageSize = 10; // 每页显示多少条记录

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

}
